package com.woniu.service.impl;

import com.woniu.mapper.CourseDao;
import com.woniu.pojo.Course;
import com.woniu.pojo.CourseExample;
import com.woniu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseDao courseDao;
    @Override
    public void add(Course course) {
        courseDao.insert(course);
    }

    @Override
    public void deleteById(Integer id) {
        courseDao.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        for (Integer id:ids) {
            courseDao.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Course findOne(Integer id) {
        Course course = courseDao.selectByPrimaryKey(id);
        return course;
    }

    @Override
    public void updateById(Course course) {
        courseDao.updateByPrimaryKeySelective(course);
    }

    @Override
    public List<Course> selectByExample(Course course) {
        CourseExample example = new CourseExample();
        CourseExample.Criteria criteria = example.createCriteria();
        if(course.getCourseName()!=null&&!"".equals(course.getCourseName())){
            criteria.andCourseNameLike('%'+course.getCourseName()+'%');
        }
        if(course.getCourseAttribute()!=null&&!"".equals(course.getCourseAttribute())){
            criteria.andCourseAttributeLike('%'+course.getCourseAttribute()+'%');
        }
        List<Course> courseList = courseDao.selectByExample(example);
        return courseList;
    }

    @Override
    public List<Course> selectAll() {
        return courseDao.selectByExample(null);
    }

    @Override
    public List<Course> selectByCourseIdList(List<Integer> courseIdList) {
        CourseExample example = new CourseExample();
        CourseExample.Criteria criteria = example.createCriteria();
        if (courseIdList.size()>0){
            criteria.andCourseIdIn(courseIdList);
        }
        return courseDao.selectByExample(example);
    }
}
